import { useRequest } from 'ahooks'
import type { Options } from 'ahooks/lib/useRequest/src/types'
import axios from 'axios'

import './__mock__'

/** 客户 企业联系人 */
export interface IEnterpriseConnector {
  id: number
  firstName: string | null
  lastName: string | null
  enterpriseName: string | null
  $showName: string
}

interface TRes {
  list: IEnterpriseConnector[]
}

/** SWR 客户 获取企业联系人列表 */
export const useEnterpriseConnectors = (opts?: Options<TRes, []>) => {
  return useRequest<TRes, []>(
    async () => {
      const res = await axios.get(`/api/admin/enterprise/connectors`)
      let list = (res.$data || []) as IEnterpriseConnector[]
      list = list.map(d => ({
        ...d,
        $showName: [[d.firstName, d.lastName].filter(Boolean).join(' '), d.enterpriseName].filter(Boolean).join(' - '),
      }))
      return { list }
    },
    {
      cacheTime: -1, // 永不过期
      staleTime: 1000 * 60 * 5, // 5 分钟新鲜
      ...opts,
      cacheKey: 'SWRApi_useEnterpriseConnectors',
    },
  )
}
